<?php
declare (strict_types=1);

namespace app\exam\logic;

use app\common\model\ProductType;
use app\exam\model\Examination as ExaminationModel;
use app\exam\model\PaperConfig;
use think\db\exception\DbException;
use think\Paginator;

/**
 * 公开考试练习逻辑
 */
class Examination extends ExaminationModel
{

    /**
     * 获取公开考试练习列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-03
     *
     * @param  [type] $param [description]
     * @return Paginator
     * @throws DbException
     */
    public function getExaminationList($param): Paginator
    {
        // 验证数据
        $this->getValidate()->scene('examination_list')->failException()->check($param);
        // 参数
        $module_id = $param['module_id'];
        $category  = $param['category'] ?? 0;
        $map[]     = ['module_id', '=', $module_id];
        $category && $map[] = ['paper_id', 'IN', app(Paper::class)->whereLike('category', '%,' . $category . ',%')->column('id')];

        // 获取试卷列表
        return $this->with(['paper_base'])->withCount(['has_bought' => 'has_bought', 'paper_answer' => 'post_times'])->where($map)->field('paper_id')->paginate(static::getListRows())->each(function ($item) {
            // 考试配置
            $config = app(PaperConfig::class)->where('data_key', 'examination_' . $item->module_id)->where('paper_id', $item->paper_id)->field('cnf_data')->findOrEmpty();
            // 数据处理
            $item->start_time  = $config['cnf_data']['exam_stime'] ?? 0;
            $item->end_time    = $config['cnf_data']['exam_etime'] ?? 0;
            $item->exam_number = $config['cnf_data']['exam_number'] ?? 0;
            $item->price       = $config['cnf_data']['price'] ?? 0;
            // 是否能够考试
            if ($item->start_time && $item->end_time) {
                $item->exam_status = ($item->start_time < time() && $item->end_time > time()) ? 1 : 0;
            } else if (!$item->start_time && !$item->end_time) {
                $item->exam_status = 1;
            } else {
                $item->exam_status = $item->start_time ? ($item->start_time < time() ? 1 : 0) : ($item->end_time > time() ? 1 : 0);
            }
            // 人脸开关
            $item->face_verification = $config['cnf_data']['face_verification'] ?? 0;

            $item->exam_rest_num = $item->exam_number ? max($item->exam_number - $item->post_times, 0) : -1;
        });
    }

    /**
     * 搜索公开考试列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-21
     *
     * @param  [type] $param [description]
     */
    public function search($param)
    {
        // 验证数据
        $this->getValidate()->scene('examination_search')->failException()->check($param);
        // 参数
        $module_id = $param['module_id'];
        $title     = $param['title'];
        // 获取试卷列表
        return $this->join('exam_paper paper', 'paper.id=paper_id')->where('title', 'LIKE', '%' . $title . '%')->withCount(['has_bought' => 'has_bought', 'paper_answer' => 'post_times'])->whereModuleId($module_id)->paginate(static::getListRows())->each(function ($i) {
            if ($i['start_time'] && $i['end_time']) {
                $i->exam_status = ($i['start_time'] < time() && $i['end_time'] > time()) ? 1 : 0;
            } else if (!$i['start_time'] && !$i['end_time']) {
                $i->exam_status = 1;
            } else {
                $i->exam_status = $i['start_time'] ? ($i['start_time'] < time() ? 1 : 0) : ($i['end_time'] > time() ? 1 : 0);
            }
            $i->exam_rest_num = $i->exam_number ? max($i->exam_number - $i->post_times, 0) : -1;
        })->visible(['module_id', 'price', 'paper_id', 'status', 'has_bought', 'post_times', 'title', 'total_count', 'start_time', 'end_time', 'exam_number', 'exam_status', 'exam_rest_num']);
    }

    /**
     * 试卷分类
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-05
     *
     * @param  [type] $param [description]
     */
    public function getExaminationCategory($param)
    {
        return app(Paper::class)->getPaperCategory($param);
    }

    /**
     * 订单产品详情
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-02
     *
     * @param  [type] $orderItem [description]
     */
    public function getProductInfo($orderItem)
    {
        $info = $this->with(['paper_title', 'paper_mhm'])->field('id,paper_id,module_id')->findOrEmpty($orderItem->product_id);

        $info->type_id     = ProductType::getProductIdByKey('exam');
        $info->payment     = $orderItem->payment;
        $info->expire_time = $orderItem->expire_time;
        $info->expire_rest = $orderItem->expire_time ? ceil(max(0, $orderItem->expire_time - time()) / 86400) : -1;

        return $info;
    }

}
